package com.ruoyi.hcrf.domain.service.storage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.hcrf.bo.input.storage.StockMasterCreateInBo;
import com.ruoyi.hcrf.bo.input.storage.StorageMasterPageInBo;
import com.ruoyi.hcrf.bo.output.storage.StockMasterPageOutBo;
import com.ruoyi.hcrf.domain.StockMaster;
import com.ruoyi.hcrf.domain.mapper.storage.StockMasterMapper;
import com.ruoyi.hcrf.enums.PageFactory;
import com.ruoyi.hcrf.enums.StorageEnabledEnum;
import com.ruoyi.hcrf.enums.StorageStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 库位主档表 业务层
 *
 * @author wanglf
 */
@Service
public class StorageMasterService extends ServiceImpl<StockMasterMapper, StockMaster> {

    @Autowired
    private StockMasterMapper baseMapper;

    /**
     * 根据条件分页列表
     *
     * @param pageInBo 库位类型
     * @return 集合信息
     */
    public Page<StockMasterPageOutBo> selectPage(StorageMasterPageInBo pageInBo){
        //获取分页对象
        Page<StockMaster> page = PageFactory.createPage(pageInBo);
        return baseMapper.selectStorageMasterPage(page,pageInBo);
    }

    /**
     * 新增保存
     *
     * @param createInBo 新增信息
     * @return 结果
     */
    public Boolean insertStorageMaster(StockMasterCreateInBo createInBo)
    {
        // 新增
        StockMaster insertBo = BeanUtil.copyProperties(createInBo, StockMaster.class);
        return save(insertBo);
    }

    /**
     * 修改
     *
     * @param createInBo 修改信息
     * @return 结果
     */
    public Boolean updateStorageMaster(StockMasterCreateInBo createInBo)
    {
        // 查询库位类型
        StockMaster byId = getById(createInBo.getLocationId());
        if (ObjectUtil.isNull(byId)) {
            throw new ServiceException("库位区域不存在");
        }


        // 判断是否为修改状态 当状态非空桶 且 是禁用时报错
        if (createInBo.getStorageEnabled() != null && createInBo.getStorageEnabled().equals(StorageEnabledEnum.UNABLE.getValue())
         && !ObjectUtil.equal(byId.getStorageStatus(), StorageStatusEnum.NULL.getValue())) {
            throw new ServiceException("非空桶不能禁用！");
        }

        // 修改库位类型
        BeanUtil.copyProperties(createInBo, byId);
        return updateById(byId);
    }

    // //将库位状态改为未占用
    public boolean updateLocation(String fromSite) {

        //将库位状态改为未占用
        QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("location_id", fromSite);
        StockMaster stockMaster = baseMapper.selectOne(queryWrapper1);

        //货物状态-1-空桶 3-退货
//        stockMaster.getStorageStatus();

        stockMaster.setStorageEnabled(0);
        stockMaster.setStorageStatus(0);
        int i = baseMapper.updateById(stockMaster);
        return i>0;
    }
}
